//
//  Album.m
//  UI_15_下午练习老师版本
//
//  Created by li cheng on 16/8/4.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "Album.h"
#import "CollectionViewCell.h"
@interface Album()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end
@implementation Album
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setup];
    }
    return self;
}
- (void)setup{
    [self createCollectionView];
    [self createPageControl];
}
- (void)createCollectionView{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    // 注册cell
//    [_collectionView registerNib:@"UICollectionViewCell" forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    NSLog(@"%@", _array[indexPath.row]);
//    cell.cycleImage.image = [UIImage imageNamed:self.array[indexPath.row]];
    return cell;
}
-(void)createPageControl{
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
}
- (void)setArray:(NSArray *)array{
    _array = array;
    [_collectionView reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
@end






@implementation ImageCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _cycleImage = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_cycleImage];
        _cycleImage.image = [UIImage imageNamed:@"1"];
    }
    return self;
}

@end








